package com.chinasoftinc.facommon.view;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.ace.ability.AceAbility;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;
import ohos.rpc.RemoteException;

import com.chinasoftinc.facommon.ResourceTable;
import com.chinasoftinc.facommon.utils.LogUtil;

/**
 * @since 2022-06-28
 */
public class BaseAceAbility extends AceAbility {
    private CommonEventSubscriber subscriber;
    protected final String TAG = getClass().getSimpleName();
    public static final String EVENT_RESTART_ACTION = "RestartAbility";

    @Override
    public void onStart(Intent intent) {
        intent.setParam("semi_modal_color", getResColor(ResourceTable.Color_semi_modal_background));
        super.onStart(intent);
        subscribeRestartEvent();
    }

    private int getResColor(int resId) {
        try {
            return getResourceManager().getElement(resId).getColor();
        } catch (Throwable e) {
            return 0;
        }
    }

    public void sendRestartEvent() {
        try {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withAction(EVENT_RESTART_ACTION)
                .build();
            intent.setOperation(operation);
            CommonEventData eventData = new CommonEventData(intent);
            CommonEventManager.publishCommonEvent(eventData);
            LogUtil.info(TAG, "Publish succeeded");
        } catch (RemoteException e) {
            LogUtil.error(TAG, "Exception occurred during publishCommonEvent invocation");
        }
    }

    private void subscribeRestartEvent() {
        MatchingSkills matchingSkills = new MatchingSkills();
        if (requestRestart()) {
            matchingSkills.addEvent(EVENT_RESTART_ACTION);
        }
        addSubscribeEvent(matchingSkills);
        subscriber = new EventSubscriber(new CommonEventSubscribeInfo(matchingSkills));
        try {
            CommonEventManager.subscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    protected class EventSubscriber extends CommonEventSubscriber {
        private EventSubscriber(CommonEventSubscribeInfo subscribeInfo) {
            super(subscribeInfo);
        }

        @Override
        public void onReceiveEvent(CommonEventData commonEventData) {
            Intent intent = commonEventData.getIntent();
            String action = intent.getAction();
            LogUtil.info(TAG, "onReceiveEvent, action: " + action);
            if (EVENT_RESTART_ACTION.equals(action)) {
                terminateAbility();
            } else {
                BaseAceAbility.this.onReceiveEvent(commonEventData);
            }
        }
    }

    protected void addSubscribeEvent(MatchingSkills matchingSkills) {
    }

    protected void onReceiveEvent(CommonEventData eventData) {
        LogUtil.warn(TAG, "Un handle event: " + eventData + ", You should override the method");
    }

    protected boolean requestRestart() {
        return true;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (subscriber != null) {
            try {
                CommonEventManager.unsubscribeCommonEvent(subscriber);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
